import wepy from 'wepy'
export default class extends wepy.mixin {
	data = {
		//   会员id
		userId: '',
		//区域id
		id: '0',
		//小区id
		xid: '',
		//楼id
		lid: '',
		//单元id
		yid: '',
		//门牌号id
		mid: '',
		name: '',
		phone: '',
		quyu: '请选择您的区域信息',
		xiaoqu: '请选择您的小区信息',
		menpai: '请选择您的楼号/组',
		danyuan: '请选择您的单元',
		menhao: '请选择您的门牌号',
		//区域
		columns1: [],
		columnspp: [],
		//小区
		columns2: [],
		columnsww: [],
		//楼号、组
		columns: [],
		columnsvv: [],
		//单元
		columns3: [],
		columnscc: [],
		//门牌号
		columns4: [],
		columnshh: [],
		show: false,
		show1: false,
		show2: false,
		show3: false,
		show4: false,
		// 地址id
		addressId: ''
	}
	onLoad(options) {
		console.log(options)
		this.userId = wepy.getStorageSync('userId')
		if(options.did){
			this.getaddressList(options.did)
		}else{
			this.getAddressInfo('0',0)
		}
	}
	onShow() {
		
	}
	methods = {
		onChange(event) {
			this.name = event.detail
		},
		phoneChange(event) {
			this.phone = event.detail
		},
		// 区域
		area() {
      if(this.name == '' || this.phone == '') {
        return wepy.noToast('请输入姓名或者手机号')
      }
			this.show1 = true
		},
		onCancel1() {
			this.show1 = false
		},
		onConfirm1(event) {
			this.show1 = false
			this.quyu = event.detail.value
			var i = event.detail.index
			if (this.columnspp != '') {
				this.xiaoqu='请选择您的小区信息';
				this.menpai='请选择您的楼号/组';
				this.danyuan='请选择您的单元';
				this.menhao='请选择您的门牌号';
				this.xid='';
				this.lid='';
				this.yid='';
				this.mid='';
				this.id = this.columnspp[i].id
				this.getAddressInfo(this.id, 1)
				console.log("1111111111")
			} else {
				this.columns1 = []
				console.log("222222222")
			}
		},
		// 小区
		plot() {
			if(this.columnsww == '') {
        return wepy.noToast('请选择区域')
      }
      this.show2 = true
		},
		onCancel2() {
			this.show2 = false
		},
		onConfirm2(event) {
			this.show2 = false
			this.xiaoqu = event.detail.value
			var i = event.detail.index
			if (this.columnsww != '') {
				this.menpai='请选择您的楼号/组';
				this.danyuan='请选择您的单元';
				this.menhao='请选择您的门牌号';
				this.lid='';
				this.yid='';
				this.mid='';
				this.xid = this.columnsww[i].id
				this.getAddressInfo(this.xid, 2)
				console.log("3333333")
			} else {
				this.columns2 = []
				console.log("44444444444")
			}
		},
		//楼号/组
		doorplate() {
      if(this.columnsvv == '') {
        return wepy.noToast('请选择小区')
      }
			this.show = true
		},
		onCancel() {
			this.show = false
		},
		onConfirm(event) {
			this.show = false
			this.menpai = event.detail.value
			var i = event.detail.index
			if (this.columnsvv != '') {
				this.danyuan='请选择您的单元';
				this.menhao='请选择您的门牌号';
				this.yid='';
				this.mid='';
				this.lid = this.columnsvv[i].id
				this.getAddressInfo(this.lid, 3)
			} else {
				this.columns = []
			}
		},
		//单元
		bidyuan() {
      if(this.columnscc == '') {
        return wepy.noToast('请选择楼号/组')
      }
			this.show3 = true
		},
		onCancel3() {
			this.show3 = false
		},
		onConfirm3(event) {
			this.show3 = false
			this.danyuan = event.detail.value
			var i = event.detail.index
			if (this.columnscc != '') {
				this.menhao='请选择您的门牌号';
				this.mid='';
				this.yid = this.columnscc[i].id
				this.getAddressInfo(this.yid, 4)
			} else {
				this.columns3 = []
			}
		},
		//门牌号
		bidmenhao() {
      if(this.columnshh == '') {
        return wepy.noToast('请选择单元')
      }
			this.show4 = true
		},
		onCancel4() {
			this.show4 = false
		},
		onConfirm4(event) {
			this.show4 = false
			this.menhao = event.detail.value
			var i = event.detail.index
			if (this.columnshh != '') {
				this.mid = this.columnshh[i].id
			} else {
				this.columns4 = []
			}
		},
		//提交
		submits() {
			var phonetel = /^1[0-9]{10}$/;
			console.log(
				this.userId,
				this.quyu,
				this.xiaoqu,
				this.xid,
				this.name,
				this.phone,
				this.menpai,
				this.danyuan,
				this.menhao
			)
			if (this.name == '') {
				wepy.noToast('请填写您的姓名')
			} else if (!phonetel.test(this.phone)) {
				wepy.noToast('请填写正确的手机号')
			} else if (this.id == '') {
				wepy.noToast('请选择您的区域信息')
			} else if (this.xid == '') {
				wepy.noToast('请选择您的小区信息')
			} else if (this.lid == '') {
				wepy.noToast('请选择您的楼号/组')
			} else if (this.yid == '') {
				wepy.noToast('请选择您的单元')
			} else if (this.mid == '') {
				wepy.noToast('请选择您的门牌号')
			} else {
				this.aditAdress()
			}
		},
	}
	//   新增地址数据
	async aditAdress() {
		const res = await wepy.post('baseRest/setShAddr', {
			// 会员id
			memberId: this.userId,
			//   地址id
			id: this.addressId,
			//   区域
			area: this.quyu,
			//   小区
			community: this.xiaoqu,
			//   小区id
			communityId: this.xid,
			//   收货人
			rName: this.name,
			//   收货人电话
			linkPhone: this.phone,
			
			building: this.menpai,
			//   单元
			unit: this.danyuan,
			//   门牌号
			houseNumber: this.menhao,
			//区域id
			areaId:this.id,
			//   楼栋id
			buildingId:this.lid,
			//单元id
			unitId:this.yid,
			//门牌号id
			houseNumberId:this.mid
		})
		console.log(res)
		if (res.data.state == 1) {
			wepy.baseToast('操作成功')
			wepy.redirectTo({
				url: '../../pages/editUser/edituser',
			})
		} else {
			wepy.baseToast(res.data.message)
		}
	}

	// 获取地址信息
	async getAddressInfo(id, index) {
		const res = await wepy.post('baseRest/findWuyeAreaList', {
			parentId: id,
		})
		if (res.statusCode != 200) {
			return wepy.noToast('获取数据信息失败')
		} else {
			var data = res.data.data
			if (index == 0) {
				//区域
				this.columnspp=[]
				this.columns1=[]
				console.log(data, '---------')
				if (this.columnspp == '') {
					this.columnspp = res.data.data
				}
				this.columns1 = []
				for (var i = 0; i < data.length; i++) {
					this.columns1.push(data[i].name)
				}
			} else if (index == 1) {
				//小区
				this.columnsww=[]
				this.columns2=[]
				
				if (this.columnsww == '') {
					this.columnsww = res.data.data
				}
				this.columns2 = []
				for (var i = 0; i < data.length; i++) {
					this.columns2.push(data[i].name)
				}
				
				this.columnsvv=[]
				this.columns=[]
				
				this.columnscc=[]
				this.columns3=[];
				
				this.columnshh=[]
				this.columns4=[];
				
			} else if (index == 2) {
				//楼
				console.log(data, '---------')
				// this.columnsvv=[]
				this.columns=[];
				if (this.columnsvv == '') {
					this.columnsvv = res.data.data
				}
				this.columns = []
				for (var i = 0; i < data.length; i++) {
					this.columns.push(data[i].name)
				}
				
				// this.columnscc=[]
				this.columns3=[];
				
				this.columnshh=[]
				this.columns4=[];
			} else if (index == 3) {
				//单元
				console.log(data, '---------')
				// this.columnscc=[]
				this.columns3=[];
				if (this.columnscc == '') {
					this.columnscc = res.data.data
				}
				this.columns3 = []
				for (var i = 0; i < data.length; i++) {
					this.columns3.push(data[i].name)
				}
				
				this.columnshh=[]
				this.columns4=[];
			} else if (index == 4) {
				//门牌号
				console.log(data, '---------')
				this.columnshh=[]
				this.columns4=[];
				if (this.columnshh == '') {
					this.columnshh = res.data.data
				}
				this.columns4 = []
				for (var i = 0; i < data.length; i++) {
					this.columns4.push(data[i].name)
				}
			}
		}
	}
	// 根据地址id获取地址id
	async getaddressList(id) {
		const res = await wepy.post('baseRest/getAddressDetail', {
			id:id,
		})
		if (res.statusCode != 200) {
			return wepy.noToast('获取地址信息失败')
		}
		console.log(res,"000000")
		var data=res.data.data;
		this.addressId=data.id;
		this.name = data.reName;
		this.phone = data.linkPhone;
		this.quyu = data.area;
		this.xiaoqu = data.community;
		this.xid=data.communityId;
		this.menpai = data.building;
		this.danyuan = data.unit;
		this.menhao = data.houseNumber;
		this.id= data.areaId;
		//楼id
		this.lid=data.buildingId;
		//单元id
		this.yid=data.unitId;
		//门牌号id
		this.mid=data.houseNumberId;
		this.getAddressInfo('0',0)
		this.getAddressInfo(this.id, 1)
		this.getAddressInfo(this.xid, 2)
		this.getAddressInfo(this.lid, 3)
		this.getAddressInfo(this.yid, 4)
	}
}
